// subPackages/packageA/operatingRisk/stockRightPledage/stockRightPledage.ts
import { ZCC_TYPE } from "../../../../../typings/types/common";
import { Operation } from "../../../../../typings/types/operation";
import { getStockPledgeDropDownListAPI, getStockPledgeListAPI } from "../../api/operatingRisk";
import { ZCC } from "../../../../utils/BasePage";
import { formateDigit } from "../../../../utils/util";

interface PageData {
  companyId: string,
  status?: string,
  dropdownDatas: ZCC_TYPE.Option[]
}
new ZCC.RefreshListPage<any>({

  onChange() {
    this.onPullDownRefresh()
  },

  async getDropdownDatas() {
    try {
      const res = await getStockPledgeDropDownListAPI(this.data.companyId)
      if (res.content) {
        console.log(res.content)
        let items: ZCC_TYPE.Option[] = [{ label: '全部', value: '' }]
        items = items.concat(res.content.stkPawnStatusList.map((item) => {
          return {
            label: item,
            value: item
          }
        }))
        this.setData({
          dropdownDatas: items
        })
      }
    } finally { }
  },
  async getListDatas() {
    const that = this
    this.setRefreshing()
    try {
      const res = await getStockPledgeListAPI(this.data.companyId, this.data.page, this.data.status)
      if (res.content) {
        console.log(res.content)
        let tmp = this.processListDatas(res.content)
        that.updateDatasWithNewItems(tmp, res.content.total, res.content.hasNextPage)
      }
    } finally {
      that.endRefreshing()
    }
  },
  processListDatas(content: ZCC_TYPE.ListResult<Operation.StockPledgeList>) {

    const cells = content.list.flatMap((item: Operation.StockPledgeList) => {

      let tmp = [
        {
          title: item.stkPawnRegno || '-',
          status: item.stkPawnStatus,
          rows: [
            { title: '出质人：', val: item.stkPawnCzper, ishorizontal: true },
            { title: '质押标的企业名称：', val: item.stkPawnBdper, ishorizontal: true },
            { title: '质权人：', val: item.stkPawnZqper, ishorizontal: true },
            { title: '出质股权数额：', val: formateDigit(item.stkPawnCzamt), style: 'color:#F04040', valsubfix: ' 万元', ishorizontal: true },
            { title: '登记时间', val: item.stkPawnRegdate, sectitle: '公示时间', secval: item.publicityDate },
            { title: '注销原因', val: item.stkPawnRes, sectitle: '注销时间', secval: item.writeOffDate },
          ]
        }
      ]
      return tmp
    })
    return cells
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    companyId: '',
    dropdownDatas: [{ label: '全部', value: '' }]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getDropdownDatas()
    this.getListDatas()
  },

  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getListDatas()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getListDatas()
  },
})